package cn.training.controller;

import java.sql.SQLException;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import cn.training.bean.UserBean;
import cn.training.service.HelloWorldService;


@Controller("HelloWorldController")
@RequestMapping("/")
public class HelloWorldController {

	@Autowired
	HelloWorldService helloWorldService;
	
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model) {
    	UserBean userBean=new UserBean();
    	model.addAttribute("userBean", userBean);
        return "helloWorld";
    }
    
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="dl")
    public String initLogin(Model model ,@Valid @ModelAttribute("userBean") UserBean userBean, BindingResult results) {
    	
    	if(results.hasErrors())
      {return "helloWorld"; 
      }
      UserBean result = helloWorldService.searchUser(userBean);
     if(result!=null)
    {
     	
          List<UserBean> result1= helloWorldService.checkUser(userBean);

           model.addAttribute("list", result1);

            return "login";
    	
    }
    else{
       
    	   model.addAttribute("message", "请输入用户名或密码错误");
   
    	   return "helloWorld";
    }
        }
    

    @RequestMapping(value = "/init", method = RequestMethod.POST,params="zc")

      public String initregister(UserBean userBean, Model model) {

     // UserBean  result = new UserBean();
     // model.addAttribute("userBean", result);
      return "register";}
    
    @RequestMapping(value = "/initadd", method = RequestMethod.POST,params="register")

    public String initinsert(Model model ,@Valid @ModelAttribute("userBean") UserBean userBean, BindingResult results) throws SQLException 
    { 
    	if(results.hasErrors())
    {return"register";}
       String pass1=userBean.getPassWord();
       String pass2=userBean.getPassWordconfim();
      if(pass1.equals(pass2))
      {
    	  int insert=helloWorldService.insert(userBean);
    	  if(insert==1)
    	  {
    		  model.addAttribute("message","注册成功");
    		  return "result";
    		  
    	  }
    	  else{
    		  model.addAttribute("message","注册失败");
    		  model.addAttribute("userBean",userBean);
    		  return "register"; 
    		  }}
    	  else{
    		  model.addAttribute("message","输入两次密码不一致");
    		  model.addAttribute("userBean",userBean);
    		  return "register"; 
    		  
    	  }
      }
    @RequestMapping(value = "/initadd", method = RequestMethod.POST,params="return")

    public String returninsert(UserBean userBean, Model model) 
    { 	  
    	 return "helloWorld"; }
  
    
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
        public String delete(UserBean userBean, Model model) {
        //int result = helloWorldService.deleteUser(userBean);
   
    	return "sure";
    	}
    @RequestMapping(value = "/change", method = RequestMethod.GET)
    public String change(UserBean userBean, Model model) {
    // UserBean result = helloWorldService.searchUser(userBean);
       //  model.addAttribute("userBean", result);	
    return "xiugai";
	}
    
    @RequestMapping(value = "/deletes", method = RequestMethod.POST,params="yesdel")
        public String yesdelete(UserBean userBean, Model model) {
        int result = helloWorldService.deleteUser(userBean);
        List<UserBean> refresh=helloWorldService.checkUser(userBean);
        model.addAttribute("list",refresh);
    	return "login";
    	}

  @RequestMapping(value = "/deletes", method = RequestMethod.POST,params="nodel")
    public String nodelete(UserBean userBean, Model model) {
  
    List<UserBean> refresh=helloWorldService.checkUser(userBean);
    model.addAttribute("list",refresh);
	return "login";
	}

  
    
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String updateLogin(Model model ,@Valid @ModelAttribute("userBean") UserBean userBean, BindingResult results) 
    {	int update = helloWorldService.update(userBean);
    	if(results.hasErrors())
         {   
    		return "xiugai"; 
          }
    	
    	
    	 if(update==1)
   	  {
    		 List<UserBean> refresh=helloWorldService.checkUser(userBean);
        	 model.addAttribute("list",refresh);
        	return "login";
   		  
   	  }
   	  else{
   		  model.addAttribute("message","更新失败");
   		  model.addAttribute("userBean",userBean);
   		  return "xiugai"; }
    }}
  
   		  
    	


    
  